//
// Created by Administrator on 24-8-23.
//

#include "ShowMSG.h"
#include "cstring"
ShowMSG::ShowMSG() {

}

void ShowMSG::setHUsartHandle(UART_HandleTypeDef &usart) {
    this->usart=usart;
}

void ShowMSG::PrintMSG(string msg) {
    // 转换 std::string 为 const char*
    msg+="\r\n";
    const char* c_str = msg.c_str();

    // 获取字符串的长度
    uint16_t length = static_cast<uint16_t>(msg.length());

    // 通过 UART 发送数据
    HAL_UART_Transmit(&this->usart, reinterpret_cast<const uint8_t*>(c_str), length, HAL_MAX_DELAY);
}

void ShowMSG::PrintMSG(const char *msg) {
// 计算传入 C 字符串的长度
    uint16_t length = static_cast<uint16_t>(strlen(msg));

    // 创建一个新的字符串并添加换行符
    std::string newMsg(msg);
    newMsg += "\r\n";

    // 转换 std::string 为 const char*
    const char* c_str = newMsg.c_str();

    // 通过 UART 发送数据
    HAL_UART_Transmit(&this->usart, reinterpret_cast<const uint8_t*>(c_str), static_cast<uint16_t>(newMsg.length()), HAL_MAX_DELAY);
}


